home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 29.9 KB | 669 lines |
- /*
- * 17 1/24/97 4:27p Tjones
- * Removed 'throws SQLException' from setbinding method
- *
- * 16 1/17/97 3:14p Tjones
- * Modified setBinding method to throw SQLException
- *
- * 15 12/19/96 1:42p Tjones
- * Bug fix with the setTreatBlankAs method
- *
- * 14 12/18/96 2:53p Tjones
- * Minor cleanup of unused variables and stuff.
- *
- * 13 12/16/96 1:50p Cthrons
- * Moved list binding code from Applet into component.
- * Make static list and lookup work together.
- * Call raiseException instead of System.out.println.
- * Call makeVisible in setText.
- *
- * 12 12/16/96 9:54a Tjones
- * Added to parameter list for call to setValueFromString.
- * Add setTreatBlankAs method.
- *
- * 11 12/08/96 1:30a Cthrons
- * Maintain and use m_IsBound variable.
- * Pass ProjBinder to notifyInputChanged.
- *
- * 10 12/08/96 12:08a Cthrons
- * Copied standard proj link methods from TextField
- *
- * 9 12/07/96 6:17p Tjones
- * Changed return type of notifySetData to boolean.
- *
- * 8 11/27/96 5:02p Kbradle
- * Add the notifySetData() methods, currently just stubs for this
- * listbinding component.
- *
- * 7 11/25/96 2:29p Tjones
- *
- * 6 11/25/96 1:40p Tjones
- *
- * 5 11/25/96 1:36p Tjones
- * Introduced dynamic edit method
- *
- * 4 11/14/96 8:22p Cthrons
- * Put in safety code to make sure binder is readable, writable, etc...
- *
- * 3 11/01/96 1:41p Cthrons
- * Implement ProjLink interface.
- * Process key input.
- *
- * 2 10/29/96 1:49p Cthrons
- * Changed the package name.
- * Fixed up notifyListChanged so that it would compile.
- *
- * 1 9/25/96 5:17p Spollac
- * an object that implements the ListLink interface
- */
-
- package symantec.itools.db.awt;
-
- import java.util.*;
- import symjava.sql.*;
- import java.lang.*;
- import symantec.itools.db.net.*;
- import symantec.itools.db.pro.*;
-
- public class List extends java.awt.List implements ListLink, ProjLink
- {
-
- /**
- * The Binder object which this component can use to get data from
- * the server, or change data at the database server.
- */
- private ListBinder m_ListBinder;
-
- /**
- * The ProjBinder object which this component can use to get data from
- * the server, or change data at the database server.
- */
- private ProjBinder m_ProjBinder;
-
- private boolean m_DynamicUpdate = false;
- private String m_BinderData; // Keep in synch with database
- private String m_ScreenData; // Keep in synch with screen text
- private boolean m_useStream = false;
- private boolean m_IsBound = false;
- private int m_treatBlankAs = 0;
-
- private RelationView m_LookUpRelationView;
- private Vector m_JoinColumns;
- private ConnectionInfo m_Conn;
- private String m_SQL;
-
- //*************************************************************************//
- // //
- // FUNCTION: List //
- // //
- // PURPOSE: //
- // //
- // PARAMETERS: //
- // //
- // RETURNS: //
- // //
- // COMMENTS: //
- // //
- //*************************************************************************//
-
- public List(RelationView lookupRV, ConnectionInfo conn)
- {
- this();
- m_LookUpRelationView = lookupRV;
- m_Conn = conn;
- }
-
- /**
- * Constructs a new List with no data connectivity, initially.
- *
- * @param rows defines the number of rows of items to display.
- */
- public List(int rows)
- {
- super(rows, false);
- m_ScreenData = new String();
- m_BinderData = new String();
- m_JoinColumns = new Vector();
- m_SQL = new String();
- }
-
- /**
- * Constructs a new List with no data connectivity, initially.
- */
- public List()
- {
- this(0);
- }
-
- /**
- * Implement the ProjLink interface function, init().
- * This function is called when the control is first bound to data.
- *
- * @param binder The Binder binder object which this component can use
- * to change data at the database server.
- */
- public void init (ListBinder lBinder)
- {
- m_ListBinder = lBinder;
- }
-
- /**
- * Implement the ProjLink interface function, notifyDataChange().
- * This function is called when the projection's data changes at the database.
- *
- * @param binder The Binder binder object which this component can use
- * to get or change data at the database server.
- */
- public void notifyListChange(ListBinder lBinder)
- {
- try
- {
- RelationView curRV = lBinder.getRelationView();
- clear();
- while ( curRV.next() == true )
- {
- if (!(curRV.isNull(1)))
- {
- super.addItem( curRV.getString(1));
- }
- }
- Enumeration e = staticItems.elements();
- while(e.hasMoreElements())
- {
- super.addItem((String)e.nextElement());
- }
- if (m_IsBound)
- {
- notifyDataChange(m_ProjBinder);
- }
- }
- catch (SQLException Ex)
- {
- raiseException ("Exception from List.notifyDataChange.setString: " + Ex.getMessage());
- }
- }
-
- //*************************************************************************//
- // //
- // FUNCTION: init //
- // //
- // PURPOSE: //
- // //
- // PARAMETERS: //
- // @param binder The ProjBinder binder object which this component can use//
- // to change data at the database server. //
- // //
- // RETURNS: //
- // //
- // COMMENTS: //
- // Implement the ProjLink interface function, init(). //
- // This function is called when the control is first bound to data. //
- // //
- //*************************************************************************//
-
- public void init (ProjBinder binder)
- {
- m_ProjBinder = binder;
- setEditable(m_ProjBinder);
- }
-
- public void setTreatBlankAs(String blank)
- {
- if (new String(blank).toUpperCase().equals("DEFAULT"))
- {
- m_treatBlankAs = RelationView.SETBLANKTODEFAULT;
- }
- else if (new String(blank).toUpperCase().equals("NULL"))
- {
- m_treatBlankAs = RelationView.SETBLANKTONULL;
- }
- else if (new String(blank).toUpperCase().equals("BLANK"))
- {
- m_treatBlankAs = RelationView.SETBLANKTOEMPTY;
- }
- }
-
- //*************************************************************************//
- // //
- // FUNCTION: setBinding //
- // //
- // PURPOSE: //
- // //
- // PARAMETERS: //
- // //
- // RETURNS: //
- // //
- // COMMENTS: //
- // //
- //*************************************************************************//
-
- public void setBinding(RelationView relView, String projection)
- {
- try
- {
- int projectionNumber = relView.findProjByName(projection);
- relView.bindProj(projectionNumber, this);
- }
- catch (SQLException Ex)
- {
- raiseException(
- "SQLException from TextField.notifyDataChange.getStringValue: "
- + Ex.getMessage());
- return;
- }
- m_IsBound = true;
- }
-
- //*************************************************************************//
- // //
- // FUNCTION: setDynamicUpdate //
- // //
- // PURPOSE: //
- // //
- // PARAMETERS: //
- // //
- // RETURNS: //
- // //
- // COMMENTS: //
- // //
- //*************************************************************************//
-
- public void setDynamicUpdate(boolean update)
- {
- m_DynamicUpdate = update;
- }
-
- //*************************************************************************//
- // //
- // FUNCTION: notifyDataChange //
- // //
- // PURPOSE: //
- // Implement the ProjLink interface function, notifyDataChange(). //
- // This function is called when the projection's data changes at //
- // the database. //
- // //
- // PARAMETERS: //
- // @param binder The ProjBinder binder object which this component can use//
- // to get or change data at the database server. //
- // //
- // RETURNS: //
- // //
- // COMMENTS: //
- // //
- // Step 1: set m_ProjBinder equal to binder object //
- // Step 2: synchronize m_BinderData with binder object //
- // Step 3: synchronize m_ScreenData with current screen //
- // Step 4: if the two are different then //
- // - set m_ScreenData equal to m_BinderData to avoid recursion //
- // - call setText to update screen //
- // Step 5: enable or disable editing based on binder //
- // //
- //*************************************************************************//
-
- public void notifyDataChange(ProjBinder binder)
- {
- // STEP 1
- if (binder == null)
- {
- // Raise an exception..
- return;
- }
- m_ProjBinder = binder;
-
- // STEP 2
- try
- {
- if (binder.getRelationView().getCurrentRecordState()
- != Record.RECSTATE_INVALID)
- {
- if (binder.isReadable() && !binder.isNull())
- {
- m_BinderData = binder.getStringValue();
- }
- else
- {
- m_BinderData = "";
- }
- }
- else
- {
- m_BinderData = "";
- }
- }
- catch (SQLException Ex)
- {
- raiseException(
- "SQLException from TextField.notifyDataChange.getStringValue: "
- + Ex.getMessage());
- }
- catch (java.io.IOException Ex)
- {
- raiseException(
- "IOException from TextField.notifyDataChange.getStringValue: "
- + Ex.getMessage());
- }
- if (m_BinderData == null)
- {
- m_BinderData = new String();
- }
-
- // STEP 3
- m_ScreenData = getText();
-
- // STEP 4
- if (!m_BinderData.equals(m_ScreenData))
- {
- m_ScreenData = m_BinderData;
- setText(m_ScreenData);
- }
-
- // STEP 5
- setEditable(m_ProjBinder);
- }
-
- //*************************************************************************//
- // //
- // FUNCTION: lostFocus //
- // //
- // PURPOSE: Intercept the event to send data to the binder //
- // //
- // PARAMETERS: //
- // @param evt the lost focus event being caught //
- // @param what the object that is losing focus //
- // //
- // RETURNS: //
- // //
- // COMMENTS: //
- // Call notifySetData since that is the standard way of handling //
- // any event that should cause us to send data to the binder. //
- // //
- //*************************************************************************//
-
- public boolean lostFocus(java.awt.Event evt, Object what)
- {
- if (m_IsBound)
- {
- notifySetData(m_ProjBinder);
- }
- return super.lostFocus(evt, what);
- }
-
- //*************************************************************************//
- // //
- // FUNCTION: notifyInputChange //
- // //
- // PURPOSE: Handle any event that causes us to send data to binder object //
- // //
- // PARAMETERS: //
- // //
- // RETURNS: //
- // //
- // COMMENTS: //
- // //
- // //
- // Step 1: synchronize m_ScreenData with current screen //
- // Step 2: if m_ScreenData differs from m_BinderData then... //
- // - set m_BinderData equal to m_ScreenData to avoid recursion //
- // - call m_Binder.setString to update binder //
- // //
- //*************************************************************************//
-
- boolean notifyInputChanged(ProjBinder binder, String input)
- {
- // STEP 1
- if (!m_ScreenData.equals(input))
- {
- m_ScreenData = input;
- }
-
- // STEP 2
- if (!m_BinderData.equals(m_ScreenData))
- {
- m_BinderData = m_ScreenData;
- try
- {
- binder.setValueFromString(m_ScreenData, 0, m_treatBlankAs);
- }
- catch (SQLException Ex)
- {
- raiseException(
- "SQLException from List.notifyInputChange: "
- + Ex.getMessage());
- return false;
- }
- catch (java.io.IOException Ex)
- {
- raiseException(
- "IOException from List.notifyInputChange: "
- + Ex.getMessage());
- return false;
- }
- }
- return true;
- }
-
- //*************************************************************************//
- // //
- // FUNCTION: notifySetData //
- // //
- // PURPOSE: Send data to binder //
- // //
- // PARAMETERS: //
- // @param binder The ProjBinder binder object which this component can use//
- // to change data at the database server. //
- // //
- // RETURNS: //
- // //
- // COMMENTS: //
- // //
- // //
- //*************************************************************************//
-
- public boolean notifySetData(ProjBinder binder)
- {
- return ( notifyInputChanged(binder, getText()) );
- }
-
- //*************************************************************************//
- // //
- // FUNCTION: handleEvent //
- // //
- // PURPOSE: Respond to any event we want to handle ourselves. //
- // //
- // PARAMETERS: //
- // //
- // RETURNS: //
- // //
- // COMMENTS: //
- // * Handles the event. Returns true if the event is handled and //
- // * should not be passed to the parent of this component. The default //
- // * event handler calls some helper methods to make life easier //
- // * on the programmer. //
- // * @param evt the event //
- // * @;see #mouseEnter //
- // * @see #mouseExit //
- // * @see #mouseMove //
- // * @see #mouseDown //
- // * @see #mouseDrag //
- // * @see #mouseUp //
- // * @see #keyDown //
- // * @see #action //
- // //
- //*************************************************************************//
-
- public boolean handleEvent(java.awt.Event evt) {
- if (evt.id == java.awt.Event.LIST_SELECT && m_IsBound)
- {
- notifySetData(m_ProjBinder);
- }
- return super.handleEvent(evt);
- }
-
- void setText(String text)
- {
- boolean bitemFound = false;
- for (int index = 0; index < countItems(); index++)
- {
- if (text.equals(getItem(index)))
- {
- select(index);
- makeVisible(index);
- bitemFound = true;
- break;
- }
- if (!bitemFound)
- {
- if (getSelectedIndex() != -1)
- {
- deselect(getSelectedIndex());
- }
- }
- }
- }
-
- //*************************************************************************//
- // //
- // FUNCTION: setEditable //
- // //
- // PURPOSE: Enable or disable input //
- // //
- // PARAMETERS: //
- // @param binder The ProjBinder binder object which this component can use//
- // to change data at the database server. //
- // //
- // RETURNS: //
- // //
- // COMMENTS: //
- // //
- //*************************************************************************//
-
- void setEditable(ProjBinder binder)
- {
- //Disable input if binder is not writeable
- boolean isWritable = false;
-
- try
- {
- if (binder != null)
- {
- RelationView rv = binder.getRelationView();
- if (rv != null && (rv.getCurrentRecordState() != Record.RECSTATE_INVALID))
- {
- isWritable = binder.isWritable();
- }
- }
- }
- catch (SQLException Ex)
- {
- raiseException(Ex.getMessage());
- }
- // setEditable(isWritable);
- }
-
- String getText()
- {
- String[] selectedItems = getSelectedItems();
- if (selectedItems.length == 1)
- {
- return selectedItems[0];
- }
- return "";
- }
-
- //*************************************************************************//
- // //
- // FUNCTION: raiseException //
- // //
- // PURPOSE: //
- // //
- // PARAMETERS: //
- // //
- // RETURNS: //
- // //
- // COMMENTS: //
- // //
- //*************************************************************************//
-
- void raiseException(String text)
- {
- System.out.println(text);
- }
-
- //*************************************************************************//
- // //
- // FUNCTION: join //
- // //
- // PURPOSE: //
- // //
- // PARAMETERS: //
- // //
- // RETURNS: //
- // //
- // COMMENTS: //
- // //
- //*************************************************************************//
-
- public void join(String columnName) throws SQLException
- {
- int colID = m_LookUpRelationView.findProjByName(columnName);
- if (m_JoinColumns == null)
- {
- m_JoinColumns = new Vector();
- }
- m_JoinColumns.addElement(new Integer(colID));
- }
-
- //*************************************************************************//
- // //
- // FUNCTION: setSQL //
- // //
- // PURPOSE: //
- // //
- // PARAMETERS: //
- // //
- // RETURNS: //
- // //
- // COMMENTS: //
- // //
- //*************************************************************************//
-
- public void setSQL(String sql) throws SQLException
- {
- m_SQL = sql;
- }
-
- //*************************************************************************//
- // //
- // FUNCTION: bind //
- // //
- // PURPOSE: //
- // //
- // PARAMETERS: //
- // //
- // RETURNS: //
- // //
- // COMMENTS: //
- // //
- //*************************************************************************//
-
- public void bindList() throws SQLException
- {
- m_LookUpRelationView.bindList(m_Conn, m_SQL, this, m_JoinColumns);
- }
-
-
- /**
- * Adds the specified item to the end of scrolling list.
- * @param item the item to be added
- */
- public synchronized void addItem(String item) {
- super.addItem(item, -1);
- staticItems.addElement(item);
- if (m_IsBound)
- {
- notifyDataChange(m_ProjBinder);
- }
- }
-
- private Vector staticItems = new Vector();
- }